﻿using System.Collections.Generic;
using System.Diagnostics;
using Dsl.Ped.Desktop.Messages;
using Dsl.Ped.Desktop.Views;
using System.Linq;

namespace Dsl.Ped.Desktop.ViewModels
{
    /// <summary>
    /// View model of the <see cref="MainWindowView"/>
    /// </summary>
    public class MainWindowViewModel
    {
        #region Fields

        private IEnumerable<ModuleViewModel> moduleViewModels = null;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
        /// </summary>
        public MainWindowViewModel()
        {
            ApplicationResources.Instance.Messenger.Register<ViewModuleMessage>(this, OnViewModuleMessage);
        }

        #endregion

        /// <summary>
        /// Gets View models of all modules loaded in the DSLPed
        /// </summary>
        public IEnumerable<ModuleViewModel> ModuleViewModels
        {
            get
            {
                return moduleViewModels ?? (moduleViewModels = this.GetModuleViewModels());
            }
        }

        /// <summary>
        /// Get view models of all modules that are going to be loaded into DSLPed application
        /// </summary>
        /// <returns></returns>
        private IEnumerable<ModuleViewModel> GetModuleViewModels()
        {
            var list = new List<ModuleViewModel>();

            foreach (var module in ApplicationResources.Instance.RegisteredModules)
            {
                list.Add(new ModuleViewModel(module.Name)
                {
                    Title = module.Title,
                    View = module.View
                });
            }

            return list;
        }

        /// <summary>
        /// Handler method on receive of <see cref="ViewModuleMessage"/>
        /// </summary>
        /// <param name="viewModuleMessage"><see cref="ViewModuleMessage"/> to be handled</param>
        private void OnViewModuleMessage(ViewModuleMessage viewModuleMessage)
        {
            if (viewModuleMessage == null) return;
            if (string.IsNullOrEmpty(viewModuleMessage.Name)) return;
            
            var moduleViewModel = this.GetModuleViewModelByName(viewModuleMessage.Name);
            if (moduleViewModel != null)
            {
                moduleViewModel.IsVisible = true;
            }
        }

        /// <summary>
        /// Get <see cref="ModuleViewModel"/> representing a module of a given name
        /// </summary>
        /// <param name="name">Name of the module which <see cref="ModuleViewModel"/> we are looking for</param>
        /// <returns><see cref="ModuleViewModel"/> of a corresponding module if it is registered, null otherwise</returns>
        private ModuleViewModel GetModuleViewModelByName(string name)
        {
            var moduleViewModel = ModuleViewModels.Select(x => x).Where(viewModel => viewModel.Name == name).ToList();
            
            if (moduleViewModel.Count > 1)
            {
                Debug.WriteLine("More then one modules registered under the same name " + name);
                return null;
            }
            else if (moduleViewModel.Count == 0)
            {
                return null;
            }
            else
            {
                return moduleViewModel[0];   
            }
        }
    }
}
